package com.lancoo.skegndemo;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.skegn.LancooSpeechRecognitionManager;
import com.lancoo.skegndemo.dialog.AudioRecoderDialog;
import com.lancoo.skegndemo.dialog.AudioRecoderUtils;

public class DiscemmentActivity extends AppCompatActivity {

    private static final String TAG = "DiscemmentActivity";
    private AudioRecoderDialog mRecoderDialog;
    private AudioRecoderUtils mRecoderUtils;
    protected RelativeLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discemment);
        ActionBar supportActionBar = getSupportActionBar();
        Log.e("aaaa","actionBar is null:"+(supportActionBar==null));
        supportActionBar.setTitle("语音识别");
        supportActionBar.setHomeAsUpIndicator(R.drawable.back);
        supportActionBar.setHomeButtonEnabled(true);
        supportActionBar.setDisplayHomeAsUpEnabled(true);

        findViewById(R.id.img_record).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.e(TAG,"开始识别");
                        startRecognition();
                        return true;
                    case MotionEvent.ACTION_UP:
                        Log.e(TAG,"停止识别");
                        stopRecognition(true);
                        return true;
                    case MotionEvent.ACTION_CANCEL:
                        Log.e(TAG,"取消识别");
                        stopRecognition(false);
                        return true;
                }

                return false;
            }
        });
        rootView = findViewById(R.id.rootView);
        mRecoderUtils = new AudioRecoderUtils();
        mRecoderUtils.setOnAudioStatusUpdateListener(mOnAudioStatusUpdateListener);
        mRecoderDialog = new AudioRecoderDialog(this);
        mRecoderDialog.setShowAlpha(0.98f);
    }


    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }


    private void startRecognition(){

        LancooSpeechRecognitionManager.getInstance().startSpeechRecognition(this, new LancooSpeechRecognitionManager.OnLancooSpeechRecognitionListener() {
            @Override
            public void onSpeechRecognitionStart() {
                Log.e(TAG,"onSpeechRecognitionStart");
            }

            @Override
            public void onSpeechRecogniting(int vad_status, int sound_intensity) {
                Log.e(TAG,"onSpeechRecogniting");
            }

            @Override
            public void onSpeechRecognitionEnd(String recognitionSentence, String url) {
                Log.e(TAG,"onSpeechRecognitionEnd,"+url);
                String content="识别结果:"+"\n"+ recognitionSentence;
                showRecordResult(content);
            }

            @Override
            public void onSpeechRecognitionError(String error) {
                Log.e(TAG,"onSpeechRecognitionError");
                showRecordResult("识别失败:"+"\n"+error);
            }
        });
        mRecoderUtils.startRecord();
        mRecoderDialog.showAtLocation(rootView, Gravity.CENTER, 0, 0);
    }


    private void stopRecognition(boolean isShowScore){
        LancooSpeechRecognitionManager.getInstance().stopSpeechRecognition(isShowScore);
        mRecoderUtils.stopRecord();
        if(mRecoderDialog!=null && mRecoderDialog.isShowing()){
            mRecoderDialog.dismiss();
        }
    }

    private void showRecordResult(String content){
        TextView tv_result = findViewById(R.id.tv_result);
        if (tv_result!=null){
            tv_result.setMovementMethod(ScrollingMovementMethod.getInstance());
            tv_result.setText( content);
        }
    }

    AudioRecoderUtils.OnAudioStatusUpdateListener mOnAudioStatusUpdateListener = new AudioRecoderUtils.OnAudioStatusUpdateListener() {
        @Override
        public void onUpdate(double db) {
            if (null != mRecoderDialog) {
                mRecoderDialog.setLevel((int) db);
            }
        }
    };


}
